import React,{ Component } from 'react';
import './changeClient.scss';
import '../../index/index.scss';
import { addAccountSignInit, addAccountSign } from 'api/client.js';
import { message } from 'antd';
export default class ChangeClient extends Component{
	constructor (props) {
		super(props)
		this.state={
			changeClientList: [],
			index: []
		}
	}
	goBack () {
		this.props.history.goBack()
	}
	selset (index) {
		let arr=[index];
		let arr1=this.state.index;
		if(arr1.indexOf(index) === -1){
			arr1=arr1.concat(arr);
		} else {
			arr1=this.state.index.slice(0,this.state.index.indexOf(index)).concat(this.state.index.slice(this.state.index.indexOf(index)+1))
		}
		this.setState({
			index: arr1
		})
	}
	_addAccountSign () { //给客户add标签
		// console.log(this.state.index)
		addAccountSign({
			user_ids: this.state.index,
			employee_id: 1,
			sign_id: this.props.location.state.sign_id
		}).then(res => {
			if(res.code === 1){
				message.success('添加成功！', 3 ,this.props.history.goBack())
			} else {
				message.error(res.message,3)
			}
		})
	}
	componentDidMount(){
		document.title = '选择客户';
		addAccountSignInit({ //添加客户标签初始化
			sign_id: this.props.location.state.sign_id,
			employee_id: 1,
			card_id: 1
		}).then(res => {
			if(res.code === 1){
				this.setState({
					changeClientList: res.data
				})
			}
		})
	}
	render(){
		return(
			<div className="changeClient">
				{/*<div className="title">
					<div onClick={this.goBack.bind(this)}><i className="glyphicon glyphicon-left"></i></div>
					<div>选择客户</div>
					<div className="changeClient_right" onClick={this._addAccountSign.bind(this)}>确定</div>
				</div>*/}
				<div className="changeClient_list">
					<ul className="changeClient_text">
					{
						this.state.changeClientList.map((item,index) => {
							return(
								<li onClick={this.selset.bind(this,item.user_id)} key={index}><img src={item.wx_avatar} className="changeClient_img"/><span className="changeClient_name">{item.wx_nickname}</span>
								{
									this.state.index.map((v,i) => {
										if(v === item.user_id) {
											return(
												<i className="glyphicon glyphicon-change1 icon" key={i}></i>
											)
										}
									})
								}
								</li>
							)
						})
					}
					</ul>
				</div>
				<div className="bottom">- 没有更多客户了 -</div>
				<div className="label_button" onClick={this._addAccountSign.bind(this)}>
					确定
				</div>
			</div>
		)
	}
}